const app = getApp();
Page({
	data: {
		g_iphone: '',
		height:[],
		swiper_top_bottom:1,
		swiper_item:0
	},
	banner:function(e){
		var b = e.detail.current
		// console.log(,b)
		if(b == 0){
			var a = 1;
		}else if(b == (this.data.tab_list.length-1)){
			var a = 3;
		}else{
			var a = 2;
		}
		this.setData({
			swiper_item:b,
			swiper_top_bottom:a,
			swiper_src:this.data.tab_list[b].bg_img
		})
	},
	form_id_get: function(e) {
		app.request(app.apiDomain + "index.php?c=user&a=addFormId&type=form_id", {
			form_id: e.detail.formId,
		}, function(res) {
			console.log(res.data.info);
		});
	},
	title_click:function(e){
		var that=this;
		if(that.type == e.currentTarget.dataset.id){ return false }
		console.log(e.currentTarget.dataset.id)
		wx.redirectTo({
			url: 'index?type='+ e.currentTarget.dataset.id
		})
// 		this.suo=false;
// 		// 
// 		that.index_num = 0;
// 		that.type = e.currentTarget.dataset.id;
// 		// console.log(that.type)
// 		app.request(app.apiDomain + "index.php?c=group&a=index",{
// 			page:'1',
// 			size:'20',
// 			type:that.type,
// 		}, function (res) {
// 			// console.log(res)
// // 			that.setData({
// // 				Coterie_list:''
// // 			})	
// 			// setTimeout(function(g = that, b = res.data.data.artile_list){
// 				that.setData({
// 					Coterie_list:res.data.data.artile_list,
// 					on_1:'',
// 					on_2:'',
// 					on_3:'',
// 					['on_'+ that.type]:'on',
// 					height:[],
// 					opacity:'0',
// 					scrolltop:0
// 				})
// 				that.showImg()  //调用渲染函数
// 			// },50);
// 			that.img_num = res.data.data.artile_list.length;
			// that.true_src = 0;
			// that.list_suo = 0;
		// });
		
	},
	onLoad: function(options) {
		this.index_num=1;
		this.type_num=1;
		this.suo=false;
		this.type = options.type ? options.type:2;
		this.win = 375*0.44 ;
		// this.list_suo = 0;
		this.add_data()
		console.log(options)
		this.setData({
			on_1:'',
			on_2:'',
			on_3:'',
			['on_'+this.type]:'on',
		})
	},
	add_data:function(){
		var that=this;
		console.log(that.type)
		//
		app.request(app.apiDomain + "index.php?c=group&a=index",{
			page:'1',
			size:'20',
			type:that.type,
		}, function (res) {
			var a = res.data.data;
			that.setData({
				Coterie_list:a.artile_list,
				swiper:a.banner_list,
				tab_list:a.topic_list,
				swiper_src:a.topic_list[0].bg_img,
				height:[],
				opacity:0,
			})
			//
			that.img_num = res.data.data.artile_list.length;
			// console.log(res.data.data.artile_list)
			// 监听图片加载完 获取图片的高度
			that.showImg()  //调用渲染函数
		});
		//
	},
	fen_lei_get:function(){
		this.setData({
			fen_lei_on:'on'
		})
	},
	fen_lei_off:function(){
		this.setData({
			fen_lei_on:''
		})
	},
	detail_click:function(e){
		// console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url:'detail?id=' + e.currentTarget.dataset.id
		})
	},
	video_click:function(e){
		var a = e.currentTarget.dataset.url;
		var b = e.currentTarget.dataset.id;
		app.request(app.apiDomain + "index.php?c=group&a=setincPv",{
			id:b
		},function (res) {
			console.log(res.data.ionf)
		})
		if(a){
			this.setData({
				video_src:e.currentTarget.dataset.url,
				video_on:'on'
			});
			wx.createVideoContext('video').play();
		}else{
			wx.navigateTo({
				url:'detail?id=' +  b
			})
		}
		
	},
	video_off:function(){
		// console.log(1)
		wx.createVideoContext('video').pause();
		this.setData({
			video_on:'',
			video_src:''
		});
	},
	showImg(){
		var left=0;
		var right=0;
		let height = this.data.Coterie_list;
		var that=this;
		// console.log(height)
		for(var i in height){
			var num = parseFloat(height[i].height / (height[i].width / this.win))
			// console.log(height)
			if(left<=right){
				this.setData({
					['Coterie_list[' + i + '].addtime']:'0',
				});
				left = left + num;
			}else{
				this.setData({
					['Coterie_list[' + i + '].addtime']:'1',
				});
				right =  right + num;
			};
			// console.log(left,right)
			// console.log(this.data.Coterie_list[i].addtime)
		};
		setTimeout(function(e=that){
			e.setData({
				opacity:'1',
				// scrolltop:e.true_src
			})
			// console.log(e.data.scrolltop)
		},200);
		// console.log(height)
	},
	onReady: function() {
		app.SystemInfo(this)
	},
	onShareAppMessage: function() {
		return app.onShareAppMessage();
	},
// 	onPageScroll:function(e){
// 		this.setData({
// 			scroll_num:e.detail.scrollTop
// 		})
// 	},
	scroll:function(e){
		// console.log(e)
		if(this.suo){return false}
		var that=this;
		this.index_num++
		// console.log(this.index_num)
		app.request(app.apiDomain + "index.php?c=group&a=articleList",{
			page:that.index_num,
			size:'10',
			type:that.type,
		}, function (res) {
			console.log(res)
			that.img_num = res.data.data.length;
			// that.showImg()  //调用渲染函数
			// console.log(res.data.data.artile_list,that.data.Coterie_list)
			if(res.data.data=='[]' || res.data.data.length==0){
				wx.showToast({
					title: '已经到底了',
					icon: 'none',
					duration: 1000
				})
				that.suo=true;
			}else{
// 				that.true_src = that.data.scroll_num;
// 				var sol_Coterie_list = []
// 				sol_Coterie_list = that.data.Coterie_list;
// 				that.setData({
// 					Coterie_list:'',
// 					opacity:0
// 				});
				// console.log(res.data.data.artile_list)
				// setTimeout(function(e=that,g=res.data.data.artile_list,t=sol_Coterie_list){
					// console.log(t)
					that.setData({
						Coterie_list:that.data.Coterie_list.concat(res.data.data),
					})
					// 监听图片加载完 获取图片的高度
					that.showImg()  //调用渲染函数
				// },20);
				
			}
		});
	}
})
